package utils;

import org.junit.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

/**
 * @author liyang
 * @email liyang@51dojo.com
 * @create 2017-10-23 下午8:48
 * @description dto entity 属性生成工具
 **/
public class DtoAndEntityGenerateUtil {

    @Test
    public void test() throws IOException {

        DtoAndEntityGenerateUtil.generateDtoAndEntity("/Users/liyang/Desktop/dealChar.txt",true,null);
    }

    /**
     * 使用说明：文件路径改成自己的路径，粘贴如下所示的字符串
     * "mapping_id": "string",
     * "plan_id": "string",
     * "trade_id": "string",
     *
     * 运行后生成如下所示的字符串
     *
     * @JsonProperty("mapping_id")
     * private String mappingId;
     *
     * @JsonProperty("plan_id")
     * private String planId;
     *
     * @JsonProperty("trade_id")
     * private String tradeId;
     *
     * @param isDtoOrEntity true 生成dto false 生成entity  生成的entity自行调整顺序格式化
     * @param filePath 粘贴的文件路径
     * @param tableName 生成实体对应的表名
     *
     */
    public static void generateDtoAndEntity(String filePath, Boolean isDtoOrEntity,String tableName) throws IOException {
        // "/Users/liyang/Desktop/dealChar.txt"
        FileReader fr = new FileReader(new File(filePath));
        BufferedReader bf = new BufferedReader(fr);

        String line = null;
        StringBuilder jsonProp;//json注解值

        StringBuilder sbType;//属性类型

        StringBuilder sbProp;//属性
        StringBuilder sbJson = new StringBuilder("@JsonProperty()");

        //生成entity必须有的部分
        if(!isDtoOrEntity){
            StringBuilder sb = getMustIncludePart(tableName);
            System.out.println(sb.toString());
        }

        while ((line = bf.readLine()) != null) {
            if (line.equals("") || line.length() == 0) {
                continue;
            }
            String[] strs = line.trim().split(":");
            jsonProp = new StringBuilder(strs[0]);

            sbType = new StringBuilder(strs[1]);

            sbJson.insert(sbJson.indexOf("(") + 1, jsonProp.toString());

            if(isDtoOrEntity) {
                System.out.println(sbJson.toString());
            }

            sbProp = new StringBuilder(sbJson);
            sbProp = jsonProp.deleteCharAt(0);//删除引号
            sbJson = sbProp.deleteCharAt(sbProp.length() - 1);

            while (sbProp.indexOf("_") >= 0) {
                sbProp = sbProp.replace(sbProp.indexOf("_") + 1, sbProp.indexOf("_") + 2, sbProp.substring(sbProp.indexOf("_") + 1, sbProp.indexOf("_") + 2).toUpperCase());

                sbProp.replace(sbProp.indexOf("_"), sbProp.indexOf("_") + 1, "");
            }
            String printStr = "";
            if (sbType.toString().contains("string")) {
                printStr = "private String " + sbProp.toString() + ";";
            } else if (sbType.toString().contains("date")) {
                printStr = "private Date " + sbProp.toString() + ";";
            } else if (sbType.toString().contains("int")) {
                printStr = "private int " + sbProp.toString() + ";";
            } else if (sbType.toString().contains("decimal")) {
                printStr = "private BigDecimal " + sbProp.toString() + ";";
            } else if (sbType.toString().contains("boolean")){
                printStr = "private boolean " + sbProp.toString() + ";";
            } else{
                printStr = "private " + sbProp.toString() + ";";
            }

            sbJson = new StringBuilder("@JsonProperty()");

            System.out.println(printStr + "\n");
        }


    }

    private static StringBuilder getMustIncludePart(String tableName) {
        return new StringBuilder(" private long id;\n" +
                        " private String userId;\n" +
                        " private String mappingId;\n").

                        append("  private Date createTime;\n" +
                                "    private Date lastModifyTime;\n").

                        append("    private static final String[] keyColumns = {\"id\"};\n" +
                                "    private static final String tableName = "+ tableName + ";\n" +
                                "\n" +
                                "    @Override\n" +
                                "    public String getTableName() {\n" +
                                "        return tableName;\n" +
                                "    }\n" +
                                "\n" +
                                "    @Override\n" +
                                "    public String[] getKeyColumns() {\n" +
                                "        return keyColumns;\n" +
                                "    }\n" +
                                "\n" +
                                "    @Override\n" +
                                "    public String getPrimaryVal() {\n" +
                                "        return String.valueOf(id);\n" +
                                "    }");
    }
}
